package com.ruoyi.jiajia.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JiajiaHuoDong;
import com.ruoyi.system.service.IJiajiaHuoDongService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author knd
 * @date 2021-11-04
 */
@Controller
@RequestMapping("/jiajia/huoDong")
public class JiajiaHuoDongController extends BaseController
{
    private String prefix = "jiajia/huoDong";

    @Autowired
    private IJiajiaHuoDongService jiajiaHuoDongService;

    @RequiresPermissions("jiajia:huoDong:view")
    @GetMapping()
    public String huoDong()
    {
        return prefix + "/huoDong";
    }

    /**
     * 查询活动列表
     */
    @RequiresPermissions("jiajia:huoDong:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaHuoDong jiajiaHuoDong)
    {
        startPage();
        List<JiajiaHuoDong> list = jiajiaHuoDongService.selectJiajiaHuoDongList(jiajiaHuoDong);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @RequiresPermissions("jiajia:huoDong:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaHuoDong jiajiaHuoDong)
    {
        List<JiajiaHuoDong> list = jiajiaHuoDongService.selectJiajiaHuoDongList(jiajiaHuoDong);
        ExcelUtil<JiajiaHuoDong> util = new ExcelUtil<JiajiaHuoDong>(JiajiaHuoDong.class);
        return util.exportExcel(list, "活动数据");
    }

    /**
     * 新增活动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存活动
     */
    @RequiresPermissions("jiajia:huoDong:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaHuoDong jiajiaHuoDong)
    {
        if (jiajiaHuoDong.getIsShow().intValue() == 1){
            JiajiaHuoDong search = new JiajiaHuoDong();
            search.setType(jiajiaHuoDong.getType());
            List<JiajiaHuoDong> huoDongList = jiajiaHuoDongService.selectJiajiaHuoDongList(search);
            for (JiajiaHuoDong model : huoDongList) {
                model.setIsShow(0l);
                jiajiaHuoDongService.updateJiajiaHuoDong(model);
            }
        }



        return toAjax(jiajiaHuoDongService.insertJiajiaHuoDong(jiajiaHuoDong));
    }

    /**
     * 修改活动
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaHuoDong jiajiaHuoDong = jiajiaHuoDongService.selectJiajiaHuoDongById(id);
        mmap.put("jiajiaHuoDong", jiajiaHuoDong);
        return prefix + "/edit";
    }

    /**
     * 修改保存活动
     */
    @RequiresPermissions("jiajia:huoDong:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaHuoDong jiajiaHuoDong)
    {
        if (jiajiaHuoDong.getIsShow().intValue() == 1){
            JiajiaHuoDong search = new JiajiaHuoDong();
            search.setType(jiajiaHuoDong.getType());
            List<JiajiaHuoDong> huoDongList = jiajiaHuoDongService.selectJiajiaHuoDongList(search);
            for (JiajiaHuoDong model : huoDongList) {
                model.setIsShow(0l);
                jiajiaHuoDongService.updateJiajiaHuoDong(model);
            }
        }

        return toAjax(jiajiaHuoDongService.updateJiajiaHuoDong(jiajiaHuoDong));
    }

    /**
     * 删除活动
     */
    @RequiresPermissions("jiajia:huoDong:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaHuoDongService.deleteJiajiaHuoDongByIds(ids));
    }
}
